package com.pzhu.record.controller;

import com.pzhu.record.entity.Basicinformation;
import com.pzhu.record.entity.Certificate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author Zhou YuSen
 * @date 2020/4/13 8:47
 */
@Controller
@RequestMapping("/certificate")
public class CertificateController extends BaseAction {

    @RequestMapping("/insert")
    public String insert(Certificate certificate, Model model, MultipartFile pic,
                         HttpServletRequest request,
                         @RequestParam(value = "basicinformationId",required = true) String basicinformationId) throws IOException {

            certificate.setBasicinformationId(basicinformationId);
        String filename = pic.getOriginalFilename();
        if (filename != null && filename.length() > 0) {
            String path = request.getServletContext().getRealPath("/upload");
            String imgName = path + "\\" + filename;
            File file = new File(imgName);
            pic.transferTo(file);
            certificate.setCertificatePhoto(imgName);
        }

        int insert = certificateService.insertSelective(certificate);
        if (insert != 1) {
            model.addAttribute("messageCertificate", "添加失败");
        } else {
            model.addAttribute("messageCertificate", "添加成功");
        }
        return "nav";
    }

}
